package com.xxgc.cloud.common.redis;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * @program: SpringCloudStudy
 * @description:
 * @author: Shijiayi
 * @create: 2022-12-13 16:10
 **/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Resource
    private RedisConnectionFactory factory;

    @Bean(name = "redisTemplate")
    public <K, V> RedisTemplate<K, V> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(
                LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL,
                JsonTypeInfo.As.WRAPPER_ARRAY);
        //jackson序列化默认存在不认识字段报错，用户可以自己设置将其忽略
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);


        //================================= RedisTemplate 序列化设置 start ==============================
        RedisTemplate<K, V> template = new RedisTemplate<K, V>();
        template.setConnectionFactory(redisConnectionFactory);

        //key、hashKey采用String的序列化方式
        //template.setKeySerializer(new GenericToStringSerializer(Object.class));
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        //value、HashValue采用Jackson的序列化方式
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));

        //初始化函数，使用以上设置的序列化参数
        template.afterPropertiesSet();
        //设置Redis事务一致支持，可配合@Transactional使用(需要手工关闭连接的谨慎使用)
        template.setEnableTransactionSupport(true);
        //================================= RedisTemplate 序列化设置 end ================================
        return template;
    }

    @Bean(name = "stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);

        //key、value采用String的序列化方式
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
        stringRedisTemplate.setStringSerializer(new StringRedisSerializer());

        //初始化函数，使用以上设置的序列化参数
        stringRedisTemplate.afterPropertiesSet();
        //设置Redis事务一致支持，可配合@Transactional使用(需要手工关闭连接的谨慎使用)
        stringRedisTemplate.setEnableTransactionSupport(true);
        return stringRedisTemplate;
    }


    @Bean(name = "redisTemplateTemp")
    public RedisTemplate<String, Object> redisTemplateTemp(RedisConnectionFactory redisConnectionFactory) {
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        //使用 <String, Object> 泛型，避免类型转换
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //key、hashKey采用String的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);

        //value、HashValue采用Jackson的序列化方式
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);

        //redisTemplate.setEnableDefaultSerializer(true);
        //redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        //redisTemplate.setStringSerializer(new StringRedisSerializer());

        //初始化函数，使用以上设置的序列化参数
        redisTemplate.afterPropertiesSet();
        //设置Redis事务一致支持，可配合@Transactional使用(需要手工关闭连接的谨慎使用)
        redisTemplate.setEnableTransactionSupport(true);
        return redisTemplate;
    }


}